const { exec } = require("child_process");
const fs = require("fs");

exports.filters = function(ino, stdout) {
  return new Promise(function(resolve, reject) {
    fs.writeFile(ino, stdout, err => {
      if (err) {
        return reject({ err });
      } else {
        return resolve({
          state: 0
        });
      }
    });
  });
};

exports.fsdir = pathName => {
  return new Promise(function(resolve, reject) {
    fs.readdir(pathName, (err, files) => {
      if (err) {
        return reject(err);
      } else {
        return resolve(files);
      }
    });
  });
};

exports.fsstat = pathName => {
  return new Promise(function(resolve, reject) {
    fs.stat(pathName, (err, stats) => {
      if (err) {
        return reject(err);
      } else {
        return resolve(stats);
      }
    });
  });
};

exports.getProjectTitleFromFilename = fileInputFilename => {
  if (!fileInputFilename) return "";
  // only parse title with valid scratch project extensions
  // (.sb, .sb2, and .sb3)
  const matches = fileInputFilename.match(/^(.*)\.sb[23]?$/);
  if (!matches) return "";
  return matches[1].substring(0, 100); // truncate project title to max 100 chars
};
